package com.qqtech.qquan.sys.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.controller.BaseControllerImpl;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.sys.enums.SysAreaLevelDictEnum;
import com.qqtech.qquan.sys.model.SysArea;
import com.qqtech.qquan.sys.model.SysAreaQuery;
import com.qqtech.qquan.sys.model.SysAreaVo;
import com.qqtech.qquan.sys.service.SysAreaService;

@Controller
@RequestMapping(value = "/sys/area")
public class SysAreaController extends BaseControllerImpl<SysArea, SysAreaQuery> {
	@Resource
	private SysAreaService sysAreaService;

	@Override
	protected BaseService<SysArea> getBaseService() {
		return sysAreaService;
	}

	@RequestMapping(value = "/cityMap")
	@ResponseBody
	public Result cityMap(@RequestBody SysAreaQuery sysAreaQuery) {
		Result result = new Result();
		// 查询所有城市列表
		sysAreaQuery.setLevel(SysAreaLevelDictEnum.CITY.getCode());
		Pageable pageKit3 = new PageKit(1,Integer.MAX_VALUE,new PageOrder(Direction.ASC, "firstChar"));
		List<SysArea> cityList = sysAreaService.queryList(sysAreaQuery,pageKit3);
		// 转化为有序的map
		LinkedHashMap<String, List<SysArea>> map = new LinkedHashMap<String, List<SysArea>>();
		for (SysArea sysArea2 : cityList) {
			if (null == map.get(sysArea2.getFirstChar())) {
				List<SysArea> aList = new ArrayList<SysArea>();
				aList.add(sysArea2);
				map.put(sysArea2.getFirstChar(), aList);
			} else {
				List<SysArea> aList = (List<SysArea>) map.get(sysArea2.getFirstChar());
				aList.add(sysArea2);
			}
		}
		result.put("cityMapKeys", map.keySet());
		result.put("cityMap", map);
		return result;
	}

	@RequestMapping(value = "/getAreaByParent")
	@ResponseBody
	public Result getAreaByParent(@RequestBody SysAreaQuery sysAreaQuery) {
		Result result = new Result();
		SysAreaQuery temp = new SysAreaQuery();
		temp.setParentId(sysAreaQuery.getParentId());
		List<SysAreaVo> list = sysAreaService.findAreaByParent(temp.getParentId());
		result.put("list", list);
		return result;
	}
	
	@RequestMapping(value = "/getAreaByNameAll")
	@ResponseBody
	public Result getAreaByNameAll(@RequestBody SysAreaQuery sysAreaQuery) {
		Result result = new Result();
		sysAreaQuery.setLevel(SysAreaLevelDictEnum.CITY.getCode());
		List<SysAreaVo> list=sysAreaService.queryList(sysAreaQuery);
		if(null!=list&&list.size()>0){
			String provinceName = sysAreaQuery.getProvinceName();
			if(StringUtil.isNotBlank(provinceName)) {
				for (SysAreaVo sysAreaVo : list) {
					SysArea sysArea = sysAreaService.getById(sysAreaVo.getParentId());
					String parentName = sysArea.getName();
					if(sysArea!=null&&StringUtil.isNotBlank(parentName)&&provinceName.indexOf(parentName)>=0) {
						result.put("area", sysAreaVo);
						return result;
					}
				}
			}
			result.put("area", list.get(0));
		}
		return result;
	}
}
